package son.hut.vn.app.ui.facebook;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;

import org.json.JSONException;
import org.json.JSONObject;

import son.hut.vn.app.R;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.facebook.android.AsyncFacebookRunner;
import com.facebook.android.AsyncFacebookRunner.RequestListener;
import com.facebook.android.DialogError;
import com.facebook.android.Facebook;
import com.facebook.android.Facebook.DialogListener;
import com.facebook.android.FacebookError;

@SuppressWarnings("deprecation")
public class FacebookFirst extends Activity{
	public static final String APPLICATION_ID = "279541048850439";
	private Facebook facebook;
	AsyncFacebookRunner asyncFacebookRunner;
	
	private SharedPreferences preferences;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.facebook_first);
		
		//khoi tao facebook
		facebook = new Facebook(APPLICATION_ID);
		asyncFacebookRunner = new AsyncFacebookRunner(facebook);
		
	}//28/7,4,11,18,25,1/9,8,15,22,29,6/10,13/10.
	
	public void signInFacebook(View view){
		preferences = getPreferences(MODE_PRIVATE);
	    String access_token = preferences.getString("access_token", null);
	    long expires = preferences.getLong("access_expires", 0);
	 
	    if (access_token != null) {
	        facebook.setAccessToken(access_token);
	    }
	 
	    if (expires != 0) {
	        facebook.setAccessExpires(expires);
	    }
	 
	    if (!facebook.isSessionValid()) {
	        facebook.authorize(this,
	                new String[] { "email", "publish_stream" },
	                new DialogListener() {
	 
	                    @Override
	                    public void onCancel() {
	                        // Function to handle cancel event
	                    	Log.d("TAG", "onCancel");
	                    }
	 
	                    @Override
	                    public void onComplete(Bundle values) {
	                    	
	                    	Log.d("TAG", "onComplete");
	                    	
	                        // Function to handle complete event
	                        // Edit Preferences and update facebook acess_token
	                        SharedPreferences.Editor editor = preferences.edit();
	                        editor.putString("access_token", facebook.getAccessToken());
	                        editor.putLong("access_expires", facebook.getAccessExpires());
	                        editor.commit();
	                        
	                        startActivity(new Intent(FacebookFirst.this, FacebookMain.class));
	                    }
	 
	                    @Override
	                    public void onError(DialogError error) {
	                        // Function to handle error
	                    	Log.d("TAG", "onError");
	 
	                    }
	 
	                    @Override
	                    public void onFacebookError(FacebookError fberror) {
	                        // Function to handle Facebook errors
	                    	Log.d("TAG", "onFacebookError");
	                    }
	 
	                });
		}
	}
	
	public void postToWall(View view) {
	    // post on user's wall.
	    facebook.dialog(this, "feed", new DialogListener() {
	 
	        @Override
	        public void onFacebookError(FacebookError e) {
	        }
	 
	        @Override
	        public void onError(DialogError e) {
	        }
	 
	        @Override
	        public void onComplete(Bundle values) {
	        }
	 
	        @Override
	        public void onCancel() {
	        }
	    });
	 
	}

	public void getProfileInformation(View view){
		asyncFacebookRunner.request("me", new RequestListener() {
			
			@Override
			public void onMalformedURLException(MalformedURLException e, Object state) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onIOException(IOException e, Object state) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onFileNotFoundException(FileNotFoundException e, Object state) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onFacebookError(FacebookError e, Object state) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onComplete(String response, Object state) {
				Log.d("Profile", response);
	            String json = response;
	            try {
	                JSONObject profile = new JSONObject(json);
	                // getting name of the user
	                final String name = profile.getString("name");
	                // getting email of the user
	                final String email = profile.getString("email");
	 
	                runOnUiThread(new Runnable() {
	 
	                    @Override
	                    public void run() {
	                        Toast.makeText(getApplicationContext(), "Name: " + name + "\nEmail: " + email, Toast.LENGTH_LONG).show();
	                    }
	 
	                });
	 
	            } catch (JSONException e) {
	                e.printStackTrace();
	            }
				
			}
		});
	}
}
